package com.ymatou.socialbizer.testcase;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.socialbizer.parameter.SocialFrontBean;
import com.ymatou.socialbizer.service.SocialFrontCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.mongo.NoteMongoWapper;

/**
 * 下拉提示接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SocialFront {
	private static SocialFrontBean socialfrontBean;
	private static SocialFrontCall socialfrontCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("笔记品牌和卖家主页买家秀搜索接口");
	}

	@Before
	public void caseUp() {
		socialfrontBean = new SocialFrontBean();
		socialfrontCall = new SocialFrontCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(socialfrontCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/*@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SocialFront_001() {
		Logger.start(true,"笔记品牌-验证接口返回状态");
		try {
			// TODO 补全case代码
			// org.junit.Assert.fail();
			socialfrontBean.setInfo("module:allbrand");// 返回全部品牌
			socialfrontCall.setData(socialfrontBean);
			socialfrontCall.callService();
			
			Logger.verifyEquals("OK", socialfrontCall.getStatus(), "接口返回状态正确");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}*/

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SocialFront_001() {
		Logger.start(true,"笔记品牌-检查返回品牌数据中是否包含指定品牌");
		try {
			// TODO 补全case代码
			// org.junit.Assert.fail();
			socialfrontBean.setInfo("module:allbrand,letter:A");// 返回检索为A的品牌
			socialfrontCall.setData(socialfrontBean);
			socialfrontCall.callService();
			
			JSONObject list = socialfrontCall.getBrandList();
			Logger.debug(list);
			// 判断JSONObject对象中是否包含key值为“A”的键
			boolean results = list.has("A");
			Logger.verifyEquals(true, results, "返回包含有A的品牌");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SocialFront_002() {
		Logger.start(false,"笔记品牌-当没有对应笔记的品牌，则该品牌不会出现");
		try {
			// TODO 补全case代码
			// org.junit.Assert.fail();
			socialfrontBean.setInfo("module:allbrand,letter:OP");// 没有包含OP开头的品牌的笔记，因此不会出现OP
			socialfrontCall.setData(socialfrontBean);
			socialfrontCall.callService();
			
			JSONObject list = socialfrontCall.getBrandList();
			// 判断JSONObject对象中是否包含key值‘O’的键
			boolean results = socialfrontCall.getBrandList().has("OP");
			// 数据库中不包含O开头的品牌的笔记，应该返回false
			Logger.verifyEquals(false, results, "返回不包含有O的品牌");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	/*@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SocialFront_004() {
		Logger.start(true,"卖家主页买家秀搜索接口-返回状态");
		try {
			// TODO 补全case代码
			// org.junit.Assert.fail();
			String userid = "0";
			String sellerid = "512006";// 买手id
			socialfrontBean.setFilter("and(or(term(status,0),term(userid,"
					+ userid + ")),term(sellerid," + sellerid + "))");
			socialfrontBean.setFl("noteid,userid");
			socialfrontBean.setLimit("0,10");
			socialfrontBean.setSort("desc(addtime)");
			socialfrontCall.setData(socialfrontBean);
			socialfrontCall.callService();
			Logger.comment("socialfrontCall send:"+ socialfrontCall.getEntityString());
			Logger.comment("socialfrontCall response:"+ socialfrontCall.getReturnData());
			Logger.verifyEquals("OK", socialfrontCall.getStatus(), "接口返回状态正确");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
*/
	/*@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SocialFront_005() {
		Logger.start(true,"找出买手下的所有买家秀");
		try {
			// TODO 补全case代码
			// org.junit.Assert.fail();

			socialfrontCall.setData(socialfrontBean);
			socialfrontCall.callService();
			Logger.comment("socialfrontCall send:"
					+ socialfrontCall.getEntityString());
			Logger.comment("socialfrontCall response:"
					+ socialfrontCall.getReturnData());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}*/

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SocialFront_003() {
		Logger.start(true,"卖家主页买家秀搜索接口-登录用户能查看自己未通过审核的买家秀");
		try {
			// TODO 补全case代码
			// org.junit.Assert.fail();
			// 找一个在512006下写过笔记但是还没通过的uid
			int userid = 20000305;
			int sellerid = 512006;// 买手id
			socialfrontBean.setFilter("and(or(term(status,0),term(userid,"+ userid + ")),term(sellerid," + sellerid + "))");
			socialfrontBean.setFl("noteid,userid");
			socialfrontBean.setLimit("0,10");
			socialfrontBean.setSort("desc(addtime)");
			socialfrontCall.setData(socialfrontBean);
			socialfrontCall.callService();

			// 从mongo中取10条还未通过审核的买家秀查看是否在返回中
			// 链接mongo库查询数据
			NoteMongoWapper notes = new NoteMongoWapper();
			// 通过mongo获取该卖家名下指定用户的待审核的10条笔记
			List<Map> pendNote = notes.selectLoadPart10(sellerid, 1, userid);

			int sizes = pendNote.size();// 获取返回的记录数
			// 定义一个数组用来单独存储noteid
			long[] noteIdAry = new long[sizes];
			// 将取出的10条笔记存入新数组
			for (int i = 0; i < sizes; i++) {
				noteIdAry[i] = (Long) pendNote.get(i).get("NoteId");
			}

			// 通过接口获取卖家名下的所有通过审核的笔记
			int infoSize = socialfrontCall.getNoteInfo().length();

			JSONObject[] noteAry = new JSONObject[infoSize];
			Integer[] note2Ary = new Integer[infoSize];
			for (int j = 0; j < infoSize; j++) {
				noteAry[j] = (JSONObject) socialfrontCall.getNoteInfo().get(j);

				for (int x = j; x < infoSize; x++) {
					note2Ary[x] = (Integer) noteAry[j].get("noteid");
					
				}

			}

			// noteIdAry 数组里存放的是随机10条未通过审核的数据
			// note2Ary 数组里存放的是接口返回的1000条数据

			// 保存重复数据
			Long[] repeatAry = new Long[sizes];

			// 比较两边数据是否有重合，正常情况下应该无重合数据
			for (int j = 0; j < infoSize; j++) {
				for (int i = 0; i < sizes; i++) {

					if (note2Ary[j] == noteIdAry[i]) {
						// 将重复值放入新数组
						repeatAry[i] = (Long) noteIdAry[i];
						Logger.verifyEquals(note2Ary[j], note2Ary[j], "");
					}
				}
			}

			// 通过判断保存重复数据的数组来确定是否存在重复
			if (repeatAry.length > 0) {
				Logger.verifyEquals("有重复", "有重复", "未通过审核的笔记会显示出来");
			} else {
				Logger.verifyEquals("有重复", "无重复", "未通过审核的笔记会显示出来");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SocialFront_004() {
		Logger.start(true,"未登录情况下查看卖家名下的笔记，未审核通过的不会显示");
		try {
			// TODO 补全case代码
			// org.junit.Assert.fail();
			// 未登录情况下userid传0
			int userid = 0;
			int sellerid = 512006;// 卖家id
			socialfrontBean.setFilter("and(or(term(status,0),term(userid,"
					+ userid + ")),term(sellerid," + sellerid + "))");
			socialfrontBean.setFl("noteid,userid");
			socialfrontBean.setLimit("0,10");
			socialfrontBean.setSort("desc(addtime)");
			socialfrontCall.setData(socialfrontBean);
			socialfrontCall.callService();
			
			Logger.verifyEquals("OK", socialfrontCall.getStatus(), "接口返回状态正确");
			// 链接mongo库查询数据
			NoteMongoWapper notes = new NoteMongoWapper();
			// 返回mongo里卖家名下的所有通过审核的笔记数
			int noteCount = notes.getPassCount(sellerid);
			// 返回接口中卖家名下的所有笔记数
			String totals = socialfrontCall.getTotal();
			Logger.verifyEquals(noteCount, Integer.parseInt(totals), "未登录情况下同一卖家返回的笔记数和mongo库里查到的数量一致");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SocialFront_005() {
		Logger.start(true,"未登录情况下卖家名下的笔记都为通过审核的笔记");
		try {
			// TODO 补全case代码
			// org.junit.Assert.fail();
			// 未登录情况下userid传0
			int userid = 0;
			int sellerid = 512006;// 卖家id
			socialfrontBean.setFilter("and(or(term(status,0),term(userid,"
					+ userid + ")),term(sellerid," + sellerid + "))");
			socialfrontBean.setFl("noteid,userid");
			socialfrontBean.setLimit("0,1000");
			socialfrontBean.setSort("desc(addtime)");
			socialfrontCall.setData(socialfrontBean);
			socialfrontCall.callService();
			
			// 链接mongo库查询数据
			NoteMongoWapper notes = new NoteMongoWapper();
			// 通过mongo获取该卖家名下的待审核的10条笔记
			List<Map> pendNote = notes.selectPart10(sellerid, 1);
			int sizes = pendNote.size();// 获取返回的记录数
			// 定义一个数组用来单独存储noteid
			long[] noteIdAry = new long[sizes];
			// 将取出的10条笔记存入新数组
			for (int i = 0; i < sizes; i++) {
				noteIdAry[i] = (Long) pendNote.get(i).get("NoteId");
			}

			// 通过接口获取卖家名下的所有通过审核的笔记
			int infoSize = socialfrontCall.getNoteInfo().length();

			JSONObject[] noteAry = new JSONObject[infoSize];
			Integer[] note2Ary = new Integer[infoSize];
			for (int j = 0; j < infoSize; j++) {
				noteAry[j] = (JSONObject) socialfrontCall.getNoteInfo().get(j);
				note2Ary[j] = (Integer) noteAry[j].get("noteid");


			}

			// noteIdAry 数组里存放的是随机10条未通过审核的数据
			// note2Ary 数组里存放的是接口返回的1000条数据
			int a = 0;
			
			// 比较两边数据是否有重合，正常情况下应该无重合数据
			for (int j = 0; j < infoSize; j++) {
				for (int i = 0; i < sizes; i++) {
					if (Long.valueOf(note2Ary[j]) == (Long) noteIdAry[i]) {
						// 将重复值放入新数组
						Long[] repeatAry = new Long[sizes];
						repeatAry[i] = (Long) noteIdAry[i];//该数组可以在用于调试时候使用
					   a=1;
					} 
				}
			}
			
			
			// 通过判断两个常量的值来确定是否存在重复
			if (a==1) {
				Logger.verifyEquals("无重复", "有重复", "未通过审核的笔记不会显示出来");
			} else {
				Logger.verifyEquals("无重复", "无重复", "未通过审核的笔记不会显示出来");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SocialFront_006() {
		Logger.start(true,"Ta已入手-最热");
		try {
			// TODO 补全case代码
			// org.junit.Assert.fail();
			socialfrontBean.setInfo("module:order");// 返回全部品牌
			socialfrontBean.setSort("desc(collectionnum)");//按照最热排序
			socialfrontBean.setFilter("and(or(term(status,0),term(userid,123456)))");
			socialfrontBean.setFl("userid,pics,noteid,collectionnum,content,commentnum,noteversion,addtime");
			socialfrontBean.setLimit("0,10");
			socialfrontCall.setData(socialfrontBean);
			socialfrontCall.callService();
			//是否按照热度排序
			//Logger.debug(socialfrontCall.getNoteInfo());
			int sizes = socialfrontCall.getNoteInfo().length();
			JSONObject[] noteList = new JSONObject[sizes];
			int[] oldCollectionnumList = new int[sizes];
			int[] collectionnumList = new int[sizes];//存放前10条collectionnum值
			
			for(int i=0;i<sizes;i++){
				noteList[i] = (JSONObject) socialfrontCall.getNoteInfo().get(i);
				collectionnumList[i] = (Integer) noteList[i].get("collectionnum");
				oldCollectionnumList[i] = (Integer) noteList[i].get("collectionnum");
				
			}
			
			for (int i = 0; i < sizes; i++) {
				for (int j = i; j < sizes; j++) {
					if (collectionnumList[i] < collectionnumList[j]) {
						int temp = collectionnumList[i];
						collectionnumList[i] = collectionnumList[j];
						collectionnumList[j] = temp;
					}
				}

			}
		
			Logger.verifyEquals(true,Arrays.equals(oldCollectionnumList, collectionnumList),"按热度排序");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
    
	
	/*
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SocialFront_010() {
		Logger.start(true,"Ta已入手-最新");
		try {
			// TODO 补全case代码
			// org.junit.Assert.fail();
			socialfrontBean.setInfo("module:order");// 返回全部品牌
			socialfrontBean.setSort("desc(addtime)");//按照时间排序
			socialfrontBean.setFilter("and(or(term(status,0),term(userid,123456)))");
			socialfrontBean.setFl("userid,pics,noteid,collectionnum,content,commentnum,noteversion,addtime");
			socialfrontBean.setLimit("0,10");
			
			socialfrontCall.setData(socialfrontBean);
			socialfrontCall.callService();
			
			//比对mongo返回数据 还未写
			Logger.comment("socialfrontCall send:"+ socialfrontCall.getEntityString());
			Logger.comment("socialfrontCall response:"+ socialfrontCall.getReturnData());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}*/
}